package com.moon.game.command.guest.bag;

import com.moon.game.clazz.PlayerItemsProto.PlayerItems.PlayerItem;
import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.PlayerMG;
import com.moon.game.service.PlayerItemService;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerItemVo;
import com.moon.game.vos.PlayerVo;

/**
 * @description 查看装备类道具
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-5-23 下午1:42:33
 */
public class ViewPlayerItemCmd extends BaseCmd {
	
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null || ps.length < 3) {
			doResponsePsError(req);
			return;
		}
		long playerItemId = Long.parseLong(ps[0]);
		int curPage = 0;
		if (ps.length == 2) {
			curPage = Integer.parseInt(ps[1]);
		}
		String viewId = ps[2];
		PlayerVo player = PlayerMG.instance.getPlayer(viewId);
		if (player == null) {
			req.setVO("other_id", viewId);
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_VIEW_OFFLINE, "你访问的玩家当前不在线！"));
			return;
		}
		PlayerItem playerItem = PlayerItemService.getPlayerItem(viewId, playerItemId);
		if (playerItem == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_HAVE_NO_THE_ITEM, "你查看的装备不存在，请刷新后查看！"));
			return;
		}
		PlayerItemVo vo = PlayerItemService.transDb2Vo(playerItem);
		
		if (!req.getPlayerId().equals(viewId) && !vo.isUse()) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_SYS, "你要查看指定玩家的装备可能已经被脱下，不能进行查看！"));
			return;
		}
		boolean canBeEquip = PlayerItemService.checkCanBeEquip(getPlayerVo(req), vo);
		req.setVO("can_be_equip", canBeEquip);
		req.setVO("curr_page", curPage);
		req.setVO("player_item_vo", vo);
		req.setVO("view_id", viewId);
		req.setVO("view_name", player.getName());
		response(req, req.getClientResponse());
	}
}
